<template>
  <view>
    <my-search @goSearch="goSearch" class="serachBox"></my-search>
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(carousel,index) in swiperList" :key="index">
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${carousel.goods_id}`">
          <image :src="carousel.image_src" ></image>
        </navigator>
      </swiper-item>
    </swiper>
    <view class="nav-list" >
      <view class="nav-item" v-for="(navItem,index) in navList" :key="index" @click="navHandler(navItem)">
        <image class="nav-item" :src="navItem.image_src"></image>
      </view>
    </view>
    <view class="floor-list">
      <view class="floor-item" v-for="(floor,index) in floorList" :key="index">
        <image class="floor-title" :src="floor.floor_title.image_src"></image>
        <view class="floor-img-box">
          <navigator class="left-img-box" :url="floor.product_list[0].url">
            <image :src="floor.product_list[0].image_src" :style="{width:floor.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
          </navigator>
          <view class="right-img-box">
            <navigator class="right-img-item" v-for="(item,index) in floor.product_list" :key="index" v-if="index >= 1" :url="item.url">
              <image :src="item.image_src" :style="{width:item.image_width + 'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import badgeMixin from '@/mixins/tabbar-badge.js'
  export default {
    mixins : [badgeMixin],
    data() {
      return {
        swiperList : [],
        navList : [],
        floorList: []
      };
    },
    onLoad() {
      this.getSwiperList(),
      this.getNavList(),
      this.getFloorList()
    },
    methods : {
      goSearch() {
        uni.navigateTo({
          url : '/subpkg/search/search'
        })
      },
      async getSwiperList() {
          let {data} = await uni.$http.get('/api/public/v1/home/swiperdata')
          if(data.meta.status == 200) {
            this.swiperList = data.message
          } else {
            uni.showToast({
              title: '获取轮播图失败',
              duration:1500,
              icon:"error"
            })
          }
      },
      async getNavList() {
        let {data} = await uni.$http.get('/api/public/v1/home/catitems')
        if(data.meta.status !== 200) return uni.$showMsg()
        this.navList = data.message
      },
      navHandler(item) {
        if(item.name == '分类') {
          uni.switchTab({
            url : '/pages/cate/cate'
          })
        }
      },
      async getFloorList() {
        let {data} = await uni.$http.get('/api/public/v1/home/floordata') 
        if(data.meta.status !== 200) return uni.$showMsg()
        data.message.forEach(floor => {
          floor.product_list.forEach(prop => {
            prop.url = '/subpkg/goods_list/goods_list?' + prop.navigator_url.split('?')[1]
          })
        })
        this.floorList = data.message
      }
    }
  }
</script>

<style lang="scss">
swiper-item {
  height: 330rpx;
  .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
}
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  image {
    width: 128rpx;
    height: 140rpx;
  }
}
.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}
.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
.serachBox {
  position: static;
  top: 0;
  z-index: 999;
}
</style>
